﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using Common.DBOper;

namespace VibrationTest.OffLineChart
{
    public partial class frmHistogram_offline : Form
    {
        public frmHistogram_offline()
        {
            InitializeComponent();
        }
        OperateData operdata = new OperateData();
        OperateSection opersection = new OperateSection();
        OperateTest opertest = new OperateTest();
        Dictionary<string, Series> testNameToSeries = new Dictionary<string, Series>();
        Dictionary<string, testTodata> testNameToData = new Dictionary<string, testTodata>();
        Dictionary<string, Color> testNameToColor = new Dictionary<string, Color>();
        double[] axis_txt = new double[32] {0, 10, 12.5, 16, 20, 25, 31.5, 40, 50, 63, 80, 100, 125, 160, 200, 250, 315, 400, 500, 630, 800, 1000, 1250, 1600, 2000, 2500, 3150, 4000, 5000, 6300 , 8000, 10000 };
        private void chart1_DragDrop(object sender, DragEventArgs e)
        {
            try
            {
                if (e.Data.GetDataPresent(typeof(DataGridViewSelectedRowCollection)))
                {
                    DataGridViewSelectedRowCollection rowCollection = e.Data.GetData(typeof(DataGridViewSelectedRowCollection)) as DataGridViewSelectedRowCollection;
                    if (rowCollection == null)
                    {
                        return;
                    }
                    //新增行
                    //注意要将鼠标的Point转换到当前工作区域，否则无法得到正确的HitTestInfo
                    System.Drawing.Point p = this.chart1.PointToClient(new System.Drawing.Point(e.X, e.Y));

                    //如果鼠标所在的位置的RowIndex>-1，则在当前位置接入列，否则就在最末尾新增列
                    //Console.WriteLine(this.dataGridView1.Rows[indexOfItemUnderMouseToDrag].Cells[0].Value.ToString());
                    //Console.WriteLine(rowCollection[0].Cells[0].Value.ToString());
                    //DisplayChart(this.dataGridView1.Rows[indexOfItemUnderMouseToDrag].Cells[0].Value.ToString());
                    DisplayChart(rowCollection[0].Cells[0].Value.ToString(), rowCollection[0].Cells[1].Value.ToString(), rowCollection[0].Cells[2].Value.ToString(), rowCollection[0].Cells[3].Value.ToString(), rowCollection[0].Cells[4].Value.ToString());
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        private void chart1_DragOver(object sender, DragEventArgs e)
        {
            if (!e.Data.GetDataPresent(typeof(DataGridViewSelectedRowCollection)))
            {

                e.Effect = DragDropEffects.None;
                return;
            }
            else
            {
                e.Effect = DragDropEffects.Move;  //这个值会返回给DoDragDrop方法
            }
        }


        string testname = "";
        public void DisplayChart(string channelid, string testid, string channlname,string sectionname,string testname1)
        {
               testname = sectionname+"_"+ testname1 +"_" +channlname;
            if (!channlname.Contains("1/3倍频程"))
            {
                MessageBox.Show("该图形不支持该数据类型！", "提示");
                return;
            }
            if (testNameToData.ContainsKey(testname))
            {
                MessageBox.Show("该通道的数据已经存在！", "提示");
                return;
            }
            if (testNameToData.Count>=10)
            {
                MessageBox.Show("最多只支持10个通道！", "提示");
                return;
            }
            this.chart1.Series.Clear();
            testNameToSeries.Clear();
            testNameToData.Clear();
            testNameToColor.Clear();
            Cls_Data clsdatalist = operdata.GetDataByChannelid(testid, Convert.ToByte(channelid));
            if (clsdatalist != null)
            {
              
                double[] x = new Double[clsdatalist._1_3octavedB.Count+1];
                double[] y = new Double[clsdatalist._1_3octavedB.Count+1];
                Random random = new Random();
                for (int i = 0; i <=clsdatalist._1_3octavedB.Count; i++)
                {
                    if (i == 0)
                    {
                        x[i] = 0;
                        y[i] = 0;
                    }
                    else
                    {
                        x[i] = Convert.ToDouble(i);
                        y[i] = Math.Round(Convert.ToDouble(clsdatalist._1_3octavedB[i - 1]), 3);
                    }
                }
                testTodata testdata = new testTodata();
                testdata.x_value = x.ToList();
                testdata.y_value = y.ToList();
                testdata.x_value_all = x.ToList();
                testdata.y_value_all = y.ToList();

                testNameToData.Add(testname, testdata);
                Series series = new Series();
                series.ChartType = SeriesChartType.StackedColumn;
                series.Color = ReturnColor();
                series.Name = testname;
                series.LegendText =testname;
                series.Points.DataBindXY(x,y);

                for (int i = 0; i < series.Points.Count; i++)
                {
                    series.Points[i].AxisLabel = axis_txt[i].ToString();
                }
                testNameToColor.Add(testname, series.Color);
                testNameToSeries.Add(testname, series);
                this.chart1.Series.Add(series);
                this.chart1.ChartAreas[0].AxisX.Interval = 2;
                this.chart1.ChartAreas[0].AxisY.Maximum = testdata.y_value_all.Max() * 1.5;
                this.chart1.ChartAreas[0].AxisY.Title = "单位(dB)";
                this.chart1.ChartAreas[0].AxisY.Title = "单位(Hz)";
                this.lb_channelname.Text = "通道:"+ testname;
 
            }
        }



        //返回颜色信息
        public Color ReturnColor()
        {
     

        

            if (!testNameToColor.ContainsValue(Color.Blue))
            {
                return Color.Blue;
            }

            if (!testNameToColor.ContainsValue(Color.Green))
            {
                return Color.Green;
            }


            if (!testNameToColor.ContainsValue(Color.DarkViolet))
            {
                return Color.DarkViolet;
            }


            if (!testNameToColor.ContainsValue(Color.DarkGoldenrod))
            {
                return Color.DarkGoldenrod;
            }


            if (!testNameToColor.ContainsValue(Color.Aqua))
            {
                return Color.Aqua;
            }


            if (!testNameToColor.ContainsValue(Color.Yellow))
            {
                return Color.Yellow;
            }


            if (!testNameToColor.ContainsValue(Color.Brown))
            {
                return Color.Brown;
            }


            if (!testNameToColor.ContainsValue(Color.Coral))
            {
                return Color.Coral;
            }


            if (!testNameToColor.ContainsValue(Color.DarkKhaki))
            {
                return Color.DarkKhaki;
            }


            if (!testNameToColor.ContainsValue(Color.Beige))
            {
                return Color.Beige;
            }
            else 
            {
                return Color.Chartreuse;
            }


        }


        string tishi_x = "";
        public double yb = 0.0;
        //显示游标值
        public void DispalyCursorPosition()
        {
            Point _mousePoint = Control.MousePosition;
            tishi_x = this.chart1.ChartAreas[0].CursorX.Position.ToString();
            int _x = this.chart1.PointToClient(_mousePoint).X;
            int _y = this.chart1.PointToClient(_mousePoint).Y;

            double XVuale = chart1.ChartAreas[0].AxisX.PixelPositionToValue(_x);
            double YVuale = chart1.ChartAreas[0].AxisY.PixelPositionToValue(_y);
            string tishi_y = "";
            try
            {
                if (tishi_x != "非数字" && tishi_x != null && tishi_x != "")
                {
                    tishi_y = this.chart1.Series[0].Points[Convert.ToInt32(XVuale)].YValues[0].ToString();
                }
                else
                {
                    tishi_y = "非数字";
                }



                if (this.chart1.Name == "chart1")
                {

                                string toolText = "";
                        for (int i = 0; i < this.chart1.Series.Count; i++)
                        {
                            //if (!this.chart1.Series[i].LegendText.Contains("游标") && !this.chart1.Series[i].Name.Contains("游标"))
                            //{
                                tishi_y = this.chart1.Series[i].Points[Convert.ToInt32(tishi_x)].YValues[0].ToString();
                                toolText = toolText + this.chart1.Series[i].LegendText + " X轴:" + axis_txt[Convert.ToInt32(tishi_x)].ToString() + " Y轴:" + tishi_y + "\n";
                            //}
                        }
                        this.toolTip1.Show(toolText, this.chart1, _x, _y);
                    //this.toolTip1.Show("x轴：" + tishi_x + "\n" + "y轴：" + tishi_y, this.chart1, _x, _y);

                }


                yb = Convert.ToDouble(tishi_x);
            }

            catch (Exception)
            {
                tishi_y = "";
                //this.toolTip1.RemoveAll();
            }
        }

        private void chart1_CursorPositionChanging(object sender, System.Windows.Forms.DataVisualization.Charting.CursorEventArgs e)
        {
            
            //DispalyCursorPosition();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void frmHistogram_offline_Load(object sender, EventArgs e)
        {
            this.chart1.Series.Clear();
        }

        private void chart1_CursorPositionChanged(object sender, CursorEventArgs e)
        {
            //DispalyCursorPosition();
        }
        double move_x = 0.0;
        double move_y = 0.0;
        private void chart1_MouseMove(object sender, MouseEventArgs e)
        {
            if (CursorPositionIsTrue)
            {
            move_x = e.X;
            move_y = e.Y;

            double XVuale = chart1.ChartAreas[0].AxisX.PixelPositionToValue(move_x);
            double YVuale = chart1.ChartAreas[0].AxisY.PixelPositionToValue(move_y);
            //this.chart1.ChartAreas[0].CursorX.Position = move_x;
            DispalyCursorPosition();
            }

        }
        bool CursorPositionIsTrue = false;
        private void chart1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left) 
            { 
                CursorPositionIsTrue = true; 
            } //左键
            else if (e.Button == MouseButtons.Right) 
            {

            }//右键
            else 
            {
            } //滚轮
 
        }

        private void chart1_MouseUp(object sender, MouseEventArgs e)
        {
            CursorPositionIsTrue = false;
        }


        public void RemoveToolTip()
        {
            this.toolTip1.RemoveAll();
        }
    }
}
